home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13755 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: nyssa.swt.edu!LN16674
  2. From: ln16674@nyssa.swt.edu (Leland Newsom)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: reversing a string
  5. Date: 10 Apr 1996 01:07:52 GMT
  6. Organization: Southwest Texas State University
  7. Message-ID: <4kf1l8$b18@central.server.swt.edu>
  8. References: <4k6cjl$j8f@central.server.swt.edu> <4kb1s7$6eu@ibm32.perftech.com> <829058514snz@genesis.demon.co.uk>,<1996Apr9.144011.117444@kuhub.cc.ukans.edu>
  9. Reply-To: ln16674@nyssa.swt.edu
  10. NNTP-Posting-Host: nyssa.swt.edu
  11.  
  12. In article <1996Apr9.144011.117444@kuhub.cc.ukans.edu>, anh@kuhub.cc.ukans.edu (TRAN PHAN ANH) writes:
  13. >Something like this?  Or is the s2 argument considered an extra variable.
  14. >
  15. >Anh
  16.  
  17. Yes, s2 would be considered an extra variable.  
  18. Leland
  19.  
  20.  
  21. >
  22. >
  23. >#include <stdio.h>
  24. >#include <string.h>
  25. >
  26. >void reverse(char *s1, char *s2)
  27. >{
  28. >  if(s1>=s2)
  29. >    return;
  30. >
  31. >  *s1^=*s2;
  32. >  *s2^=*s1;
  33. >  *s1^=*s2;
  34. >
  35. >  reverse(s1+1,s2-1);
  36. >}
  37. >
  38. >
  39. >int main(int argc, char *argv[])
  40. >{
  41. >  printf("Arg: %s\n",argv[1]);
  42. >
  43. >  reverse(argv[1],&argv[1][strlen(argv[1])-1]);
  44. >
  45. >  printf("Result: %s\n",argv[1]);
  46. >}
  47. >
  48. >
  49. >
  50. >
  51. >
  52. >In article <829058514snz@genesis.demon.co.uk>, Lawrence Kirby <fred@genesis.demon.co.uk> writes:
  53. >> In article <4kb1s7$6eu@ibm32.perftech.com>
  54. >>            murf@perftech.com "John Murphy" writes:
  55. >> 
  56. >>>In article <4k6cjl$j8f@central.server.swt.edu>, ln16674@nyssa.swt.edu 
  57. >>>says...
  58. >>>>
  59. >>>>I have a challenge from a friend of mine.  He wanted me to reverse a string 
  60. >>>>with recursion without using any additional variables or loops.  I got mine
  61. >>>>to work by using exclusive or, but I needed an additional variable.  Can 
  62. >>>>someone help with this problem without using the additional variable?
  63. >>>>
  64. >>>>Thanks
  65. >>>You can swap two variables, x and y, with the following series of exclusive 
  66. >>>or's:
  67. >>>        x ^= y;
  68. >>>        y ^= x;
  69. >>>        x ^= y;
  70. >> 
  71. >> From what he wrote I believe Leland was already doing that. The problem is
  72. >> to write the entire function without using an additional variable. It
  73. >> can be done! :-)
  74. >> 
  75. >> -- 
  76. >> -----------------------------------------
  77. >> Lawrence Kirby | fred@genesis.demon.co.uk
  78. >> Wilts, England | 70734.126@compuserve.com
  79. >> -----------------------------------------
  80.